#!/usr/bin/python
# -*- coding:utf-8 -*-
# 导入模块
import requests
from lxml import etree
from openpyxl import Workbook

wb = Workbook()  # 创建一个工作簿
ws_wb = wb.create_sheet(u"网站排名")  # 创建一个工作表
ws_wb['a1'] = "网站名"
ws_wb['b1'] = "域名"
ws_wb['c1'] = "Alexa周排名"
ws_wb['d1'] = "反链数"
ws_wb['e1'] = "排名"
ws_wb['f1'] = "得分"
# 确定URL地址
url = 'https://top.chinaz.com/all/index.html'
# 浏览器伪装头
header = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36'
}
# get请求
reap = requests.get(url,headers=header).text
# xpath解析
html = etree.HTML(reap)
ul = html.xpath('//*[@id="content"]/div[4]/div[3]/div/ul')
# for遍历ul中数据
for i in ul:
    title = i.xpath('./li/div[2]/h3/a/@title')  # 网站名
    gray = i.xpath('./li/div[2]/h3/span/text()')  # 域名
    alexa = i.xpath('./li/div[2]/div/p[1]/a/text()')  # Alexa周排名
    blank = i.xpath('./li/div[2]/div/p[4]/a/text()')  # 反链数
    rt = i.xpath('./li/div[3]/div/strong/text()')  # 排名
    span = i.xpath('./li/div[3]/div/span/text()')  # 得分
    list2= [i[3:8] for i in span]
    # 整合数据
    data = [list(x) for x in zip(title,gray,alexa,blank,rt,list2)]
    # for遍历
    for y in data:
        ws_wb.append(y) # 追加

wb.save('网站排名.xlsx')  # 保存

